package inputFrames;

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;

import person.Person;
import person.PersonInfo;
import person.PersonRecorder;
import constants.Constants;
import constants.Constants.INPUT_PANEL_TYPE;
import controler.FileControler;
import engine.AvartaEngine;
import frames.MainFrame;

public class InputPanel extends JPanel {
	private PersonInputPanel personInputPanel;
	private PersonInfoInputPanel personInfoInputPanel;
	private PersonRecorder personRecorder;
	private MainFrame mainFrame;
	private ButtonPanel buttonPanel;
	private AvartaEngine avartaEngine;

	public InputPanel() {
		this.setLayout(null);
		this.setSize(Constants.PANEL_SIZE_WIDTH, Constants.PANEL_SIZE_HEIGHT);
		this.setBackground(Constants.PANEL_BACKGROUND_COLOR);

		// person input panel
		personInputPanel = new PersonInputPanel();
		personInputPanel.setBounds(0, 0, Constants.INPUT_PANEL_SIZE_WIDTH, Constants.INPUT_PANEL_SIZE_HEIGHT);
		this.add(personInputPanel);
		personInputPanel.setVisible(true);

		// person info input penel
		personInfoInputPanel = new PersonInfoInputPanel();
		personInfoInputPanel.setBounds(0, 0, Constants.INPUT_PANEL_SIZE_WIDTH, Constants.INPUT_PANEL_SIZE_HEIGHT);
		this.add(personInfoInputPanel);
		personInfoInputPanel.setVisible(false);

		// button panel
		buttonPanel = new ButtonPanel();
		buttonPanel.setBounds(0, 250, Constants.INPUT_BUTTON_PANEL_SIZE_WIDTH, Constants.INPUT_BUTTON_PANEL_SIZE_HEIGHT);
		this.add(buttonPanel);

		// constructor
		personRecorder = new PersonRecorder();

		avartaEngine = AvartaEngine.getAvartaEngine();

	}

	public void init(MainFrame mainFrame) {
		this.mainFrame = mainFrame;
	}

	private class ButtonPanel extends JPanel {
		private JButton homeButton, prevButton, nextButton, saveButton;
		private ButtonEventhandler eventhandler;
		private Constants.INPUT_PANEL_TYPE currentPanel;

		public ButtonPanel() {
			this.setLayout(null);
			this.setBackground(Constants.PANEL_BACKGROUND_COLOR);

			eventhandler = new ButtonEventhandler();
			// home button
			homeButton = new JButton(new ImageIcon(Constants.HOME_IMG));
			homeButton.setActionCommand("Home");
			homeButton.addActionListener(eventhandler);
			homeButton.setBounds(20, 0, 30, 30);
			this.add(homeButton);

			// prev button
			prevButton = new JButton(new ImageIcon(Constants.PREV_IMG));
			prevButton.setActionCommand("Prev");
			prevButton.addActionListener(eventhandler);
			prevButton.setBounds(100, 0, 30, 30);
			this.add(prevButton);
			prevButton.setVisible(false);

			// next button
			nextButton = new JButton();
			nextButton.setIcon(new ImageIcon(Constants.NEXT_IMG));
			nextButton.setActionCommand("Next");
			nextButton.addActionListener(eventhandler);
			nextButton.setBounds(175, 0, 30, 30);
			this.add(nextButton);

			// save button
			saveButton = new JButton();
			saveButton.setIcon(new ImageIcon(Constants.SAVE_IMG));
			saveButton.setActionCommand("Save");
			saveButton.addActionListener(eventhandler);
			saveButton.setBounds(175, 0, 30, 30);
			this.add(saveButton);
			saveButton.setVisible(false);

			currentPanel = INPUT_PANEL_TYPE.PERSON_INPUT;
		}

		@Override
		public void paint(Graphics g) {
			super.paint(g);

			if (currentPanel == INPUT_PANEL_TYPE.PERSON_INPUT) { // 1st panel (person input)
				personInputPanel.setVisible(true);
				personInfoInputPanel.setVisible(false);
				prevButton.setVisible(false);
				nextButton.setVisible(true);
				saveButton.setVisible(false);
			} else if (currentPanel == INPUT_PANEL_TYPE.PERSON_INFO_INPUT) { // 2nd panel (person info input)
				personInputPanel.setVisible(false);
				personInfoInputPanel.setVisible(true);
				prevButton.setVisible(true);
				nextButton.setVisible(false);
				saveButton.setVisible(true);
			}
		}

		private class ButtonEventhandler implements ActionListener {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (currentPanel == INPUT_PANEL_TYPE.PERSON_INPUT) { // 1st panel (person input)

					if (e.getActionCommand() == "Next") {
						Person person = new Person();
						personInputPanel.inputInfo(person);
						personRecorder.setPerson(person);
						currentPanel = INPUT_PANEL_TYPE.PERSON_INFO_INPUT;
					} else if (e.getActionCommand() == "Home") {
						currentPanel = INPUT_PANEL_TYPE.PERSON_INPUT;
						mainFrame.setMainPanel();
					}
				} else if (currentPanel == INPUT_PANEL_TYPE.PERSON_INFO_INPUT) { // 2nd panel (person info input)
					if (e.getActionCommand() == "Prev") {
						currentPanel = INPUT_PANEL_TYPE.PERSON_INPUT;
					} else if (e.getActionCommand() == "Save") {
						PersonInfo personInfo = new PersonInfo();
						personInfoInputPanel.inputInfo(personInfo);
						personRecorder.addPersonInfo(personInfo);
						FileControler.getFileControler().save(personRecorder);
						mainFrame.setMainPanel();
					} else if (e.getActionCommand() == "Home") {
						currentPanel = INPUT_PANEL_TYPE.PERSON_INPUT;
						mainFrame.setMainPanel();
					}
				}

				repaint();
			}

		}
	}
}
